package com.sanjiahantang.youjiarole.module.evaluate.adapters;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.common.adapters.ImageAddAdapter;
import com.sanjiahantang.youjiarole.module.common.beans.FileBean;
import com.sanjiahantang.youjiarole.module.evaluate.beans.GoodsAppraiseInitResponse;
import com.sanjiahantang.youjiarole.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2017/10/28.
 */

public class GoodsEvaluateAdapter extends RecyclerView.Adapter implements StaticValue {

    private Context context;
    private LayoutInflater inflater;
    private BaseView.OnLoadListener onLoadListener;
    private List<GoodsAppraiseInitResponse.Data> listData;

    public List<GoodsAppraiseInitResponse.Data> getListData() {
        return listData;
    }

    public GoodsEvaluateAdapter(Context context, BaseView.OnLoadListener onLoadListener){
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.onLoadListener = onLoadListener;
        this.listData = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int position) {
        Object[] param = new Object[3];
        param[0] = listData.get(position);
        param[1] = onLoadListener;
        param[2] = position;
        return new InnerHolder((Activity)context,inflater.inflate(R.layout.item_goods_evaluate,null),position,param);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Object[] param = new Object[3];
        param[0] = listData.get(position);
        param[1] = onLoadListener;
        param[2] = position;
        ((InnerHolder)holder).bindData((Activity) context,position,param);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    public void setListData(List<GoodsAppraiseInitResponse.Data> listData){
        this.listData.clear();
        this.listData.addAll(listData);
        notifyDataSetChanged();
    }

    public void addImage(int position, FileBean.Data img){
        List<GoodsBean.Image> list = listData.get(position).getImageList();
        if (list == null){
            list = new ArrayList<>();
        }
        GoodsBean.Image image = new GoodsBean.Image();
        image.setUrl(img.getUrl());
        image.setId(img.getMallId());
        list.add(image);
        listData.get(position).setImageList(list);
        notifyDataSetChanged();
    }

    public void deleteImage(int id, int index) {
        List<GoodsBean.Image> list = listData.get(index).getImageList();
        for (int i = 0,max = list.size();i<max;i++){
            if (id == list.get(i).getId()){
                list.remove(i);
                break;
            }
        }
        notifyDataSetChanged();
    }

    private class InnerHolder extends RecyclerView.ViewHolder{
        private View itemView;
        public InnerHolder(Activity activity, View itemView, int position, Object[] data) {
            super(itemView);
            this.itemView = itemView;
            bindData(activity,position,data);
        }

        public void bindData(Activity activity, final int position, Object[] param){
            GoodsAppraiseInitResponse.Data data = (GoodsAppraiseInitResponse.Data) param[0];
            BaseView.OnLoadListener listener = (BaseView.OnLoadListener) param[1];
            TextView txtGoodsName = (TextView) itemView.findViewById(R.id.txtGoodsName);
            ImageView ivGoodsImage = (ImageView) itemView.findViewById(R.id.ivGoodsImage);
            txtGoodsName.setText(data.getGoodsName());
            listData.get(position).setNumber(String.valueOf(5));
            Picasso.with(activity).load(FileUtils.rePath(data.getUrl())).into(ivGoodsImage);

            EditText txtEvaluate = (EditText) itemView.findViewById(R.id.txtEvaluate);
            txtEvaluate.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                    if (charSequence != null){
                        listData.get(position).setPlace(charSequence.toString());
                    }
                }

                @Override
                public void afterTextChanged(Editable editable) {

                }
            });

            RatingBar ratingBar = (RatingBar) itemView.findViewById(R.id.ratingBar);
            ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
                @Override
                public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
                    listData.get(position).setNumber(String.valueOf((int)ratingBar.getRating()));
                }
            });

            MyGridView gvGoodsEvaluate = (MyGridView) itemView.findViewById(R.id.gvGoodsEvaluate);
            ImageAddAdapter addAdapter = new ImageAddAdapter(activity,param);
            addAdapter.addAll(data.getImageList());
            gvGoodsEvaluate.setAdapter(addAdapter);

        }
    }




}
